package com.example.bailin.giftsay.guide.adapter;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.entity.CarouselEntity;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.NetTool;
import com.squareup.picasso.Picasso;

/**
 * Created by bailin on 16/8/16.
 *
 *  轮播图适配器
 */
public class CarouselAdapter extends PagerAdapter implements ViewPager.OnPageChangeListener{
    private CarouselEntity carouselEntity;

    public void setCarouselEntity(CarouselEntity carouselEntity) {
        this.carouselEntity = carouselEntity;
    }

    @Override
    public int getCount() {
        return carouselEntity.getData().getBanners() == null ? 0: Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        NetTool tool = new NetTool();
        View view = LayoutInflater.from(MyApp.getContext()).inflate(R.layout.item_selection_carousel, container, false);
        ImageView iv = (ImageView) view.findViewById(R.id.iv_selection_carousel);
        tool.getImg(carouselEntity.getData().getBanners().get(position % carouselEntity.getData().getBanners().size()).getImage_url(), iv);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}